﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using OnTheWay.Enums;

namespace OnTheWay.Controls
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    public partial class MessageControl : UserControl
    {
        #region 属性字段

        /// <summary>
        /// 定时器
        /// </summary>
        DispatcherTimer dTimer = new DispatcherTimer();


        #endregion //属性字段

        #region 构造函数

        public MessageControl()
        {
            InitializeComponent();
        }

        #endregion //构造函数

        #region 内部方法

        /// <summary>
        /// 获取控件所在的窗口
        /// </summary>
        private Window GetLocalWindow()
        {
            FrameworkElement control = this;
            do
            {
                control = control.Parent as FrameworkElement;
            } while (!(control is Window));
            return control as Window;
        }
        #endregion //内部方法

        #region 依赖属性

        #region 属性 Message

        /// <summary>
        /// 数据源
        /// </summary>
        public String Message
        {
            set { SetValue(MessageProperty, value); if (this.Visibility != Visibility.Visible) { OnMessagePropertyChanged(Message, Message); }; }
            get { return (String)GetValue(MessageProperty); }
        }

        public static readonly DependencyProperty MessageProperty = DependencyProperty.Register(
                "Message", typeof(String), typeof(MessageControl),
                new FrameworkPropertyMetadata(null, OnMessagePropertyChanged));

        private static void OnMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MessageControl tMessageControl = d as MessageControl;
            if (tMessageControl != null && e.OldValue is String)
            {
                tMessageControl.OnMessagePropertyChanged(e.OldValue as String, e.NewValue as String);
            }
        }
        private void OnMessagePropertyChanged(String oldValue, String newValue)
        {
            if (Message != null && Message.Length > 0)
            {
                this.Visibility = Visibility.Visible;
                dTimer.Start();
            }
        }

        #endregion //Message

        #region 属性 IconSource

        /// <summary>
        /// 数据源
        /// </summary>
        public ImageSource IconSource
        {
            set { SetValue(IconSourceProperty, value);}
            get { return (ImageSource)GetValue(IconSourceProperty); }
        }

        public static readonly DependencyProperty IconSourceProperty = DependencyProperty.Register(
                "IconSource", typeof(ImageSource), typeof(MessageControl),
                new UIPropertyMetadata(new BitmapImage(new Uri("pack://application:,,,/OnTheWay;component/Images/MessageIcons/message.png"))));

        #endregion //IconSource

        #region 属性 Status

        public MessageStatusEnum Status
        {
            get { return (MessageStatusEnum)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(MessageStatusEnum),
            typeof(MessageControl), new UIPropertyMetadata(MessageStatusEnum.Message));
        

        #endregion

        #endregion //依赖属性

        #region 重新方法
        /// <summary>
        /// 控件初始化完成
        /// </summary>
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            this.Visibility = Visibility.Collapsed;

            dTimer.Tick += Control_TimerEventHandler;
            dTimer.Interval = TimeSpan.FromMilliseconds(5000);
        }
        #endregion //重新方法

        #region 事件函数
        /// <summary>
        /// 定时器事件
        /// </summary>
        private void Control_TimerEventHandler(object sender, EventArgs e)
        {
            if (sender == dTimer && IsMouseOver == false)
            {
                this.Visibility = Visibility.Collapsed;
                dTimer.Stop();
            }
        }

        #endregion

    }
}
